package com.pshare.framework.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.pshare.base.bean.SeaxResponse;
import com.pshare.constant.SeaxConstants;
import com.pshare.framework.bean.FileUploadProgress;
import com.pshare.framework.service.FileUploadService;
import com.pshare.tool.SessionTool;
import com.pshare.util.img.AverageImageScale;

@Controller
public class FileUploadController extends BaseController {
	
	@Autowired
	private FileUploadService fileUploadService;

	/**
	 * 上传图片 根据坐标 长宽 对图片进行剪裁
	 * 
	 * @param file
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/fileUpload/upload", method = RequestMethod.POST)
	@ResponseBody
	public SeaxResponse uploadPicture(MultipartFile file, String x, String y, String w, String h)
			throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		Integer xx = null, yy = null, ww = null, hh = null;
		InputStream is = null;
		if (NumberUtils.isNumber(x) && NumberUtils.isNumber(y) && NumberUtils.isNumber(w) && NumberUtils.isNumber(h)) {
			xx = Math.round(Float.parseFloat(x));
			yy = Math.round(Float.parseFloat(y));
			ww = Math.round(Float.parseFloat(w));
			hh = Math.round(Float.parseFloat(h));
		}
		if (xx != null && xx > 0 && yy != null && yy > 0 && ww != null && ww > 0 && hh != null && hh > 0) {
			// 剪裁
			is = AverageImageScale.cutImg(file.getInputStream(), xx, yy, ww, hh);
		} else {
			is = file.getInputStream();
		}
		Map<Integer, String> urlAndType = fileUploadService.uploadFile(is);
		for (Integer type : urlAndType.keySet()) {
			result.put("url", urlAndType.get(type));
			result.put("type", type);
			break;
		}
		return succeed(result);
	}

	/**
	 * 获取上传进度信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/fileUpload/getProcess", method = RequestMethod.GET)
	@ResponseBody
	public SeaxResponse getProcess(Integer code, HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Cache-Control", "no-store");// 禁止浏览器缓存
		response.setHeader("Pragrma", "no-cache");// 禁止浏览器缓存
		response.setDateHeader("Expires", 0);// 禁止浏览器缓存
		response.setContentType("text/html");
		if (code == null) {
			code = 0;
		}
		
		Object object = SessionTool.getAttribute(SeaxConstants.KEY_FIELUPLOAD_PROCESS + ":" + code);
		int percent = 0;
		if (object != null) {
			FileUploadProgress process = (FileUploadProgress) object;
			// 计算百分比
			percent = (int) ((double) process.getUploadSize() / (double) process.getTotalSize() * 100);
			return succeed(percent);
		}
		return succeed(percent);
	}

}
